package com.example.eeuse.controller;


import com.example.eeuse.mapper.DocumentMapper;
import com.example.eeuse.model.Document;
import lombok.RequiredArgsConstructor;
import org.dromara.easyes.annotation.rely.Analyzer;
import org.dromara.easyes.annotation.rely.FieldType;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.index.LambdaEsIndexWrapper;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 测试使用Easy-ES
 * 官网地址：https://www.easy-es.cn/pages/7ead0d/
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestUseEeController {
    private final DocumentMapper documentMapper;

    @GetMapping("/createIndex")
    public Boolean createIndex() {
        boolean is = documentMapper.existsIndex(Document.class.getSimpleName().toLowerCase());
        if (is){
            documentMapper.deleteIndex(Document.class.getSimpleName().toLowerCase());
        }
     /*   // 1.初始化-> 创建索引(相当于mysql中的表)
        LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
        // 此处简单起见 索引名称须保持和实体类名称一致,字母小写 后面章节会教大家更如何灵活配置和使用索引
        wrapper.indexName(Document.class.getSimpleName().toLowerCase());

        // 此处将文章标题映射为keyword类型(不支持分词),文档内容映射为text类型(支持分词查询)
        wrapper.mapping(Document::getTitle, FieldType.KEYWORD_TEXT, 2.0f)
                .mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD);

        // 设置分片及副本信息,可缺省
        wrapper.settings(3, 2);

        // 设置别名信息,可缺省
        String aliasName = "daily";
        wrapper.createAlias(aliasName);

        // 设置父子信息,若无父子文档关系则无需设置
        wrapper.join("joinField", "document", "comment");*/

        // 创建索引
        return documentMapper.createIndex();

    }

    @GetMapping("/insert/{title}/{content}")
    public Integer insert(@PathVariable String title,@PathVariable String content) {
        // 2.初始化-> 新增数据
        Document document = new Document();
        document.setTitle(title);
        document.setContent(content);
        //可转换为json
        return documentMapper.insert(document);
    }

    @GetMapping("/search/{title}/{content}")
    public List<Document> search(@PathVariable String title,@PathVariable String content) {
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Document::getTitle, title);
        wrapper.eq(Document::getContent,content);
        return documentMapper.selectList(wrapper);
    }

    @GetMapping("/update/{title}/{content}/{title1}/{content1}")
    public Integer update(@PathVariable String title,@PathVariable String content,@PathVariable String title1,@PathVariable String content1) {
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Document::getTitle, title);
        wrapper.eq(Document::getContent,content);
        List<Document> list = documentMapper.selectList(wrapper);

        // 更新信息
        Document document = new Document();
        document.setId(list.get(0).getId());
        document.setTitle(title1);
        document.setContent(content1);
        return documentMapper.updateById(document);
    }

    @GetMapping("/delete/{title}")
    public Integer delete(@PathVariable String title) {
        // 删除信息
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Document::getTitle, title);
        List<Document> list = documentMapper.selectList(wrapper);
        return documentMapper.deleteById(list.get(0).getId());
    }


    @GetMapping("/mixSearch/{title}")
    public List<Document> mixSearch(@PathVariable String title) {
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.like(Document::getTitle, title).or().like(Document::getContent, title);
//        wrapper.like(Document::getContent, title);
        return documentMapper.selectList(wrapper);
    }
    @GetMapping("/pageQuery/{title}")
    public EsPageInfo<Document> pageQuery(@PathVariable String title) {
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.match(Document::getTitle, title);
        return documentMapper.pageQuery(wrapper,1,2);
    }
}

